package com.a225.frame;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.a225.main.GameController;
import com.a225.main.GameStart;
import com.a225.model.loader.ElementLoader;
import com.a225.thread.GameThread;

public class BeginJPanel extends JPanel{
	private ImageIcon img;
	private int w;
	private int h;
	

	public BeginJPanel(){
		List<String> data = ElementLoader.getElementLoader().getGameInfoMap().get("windowSize");
		this.img = ElementLoader.getElementLoader().getImageMap().get("beginBackground");
		this.w = new Integer(data.get(0)).intValue();
		this.h = new Integer(data.get(1)).intValue();
		init();
	}

	private void init() {

		this.setLayout(null);
		
		JLabel jLabel = new JLabel(img);
		img.setImage(img.getImage().getScaledInstance(w, h,Image.SCALE_DEFAULT ));
		jLabel.setBounds(0, 0, w, h);
		
		ImageIcon img2 = new ImageIcon("img/bg/introduce.png");
		final JLabel jLabel2 = new JLabel(img2);
		jLabel2.setBounds(w/2, h/6, 600, 800);
		jLabel2.setVisible(false);
		
        //新增游戏说明面板
		ImageIcon img3 = new ImageIcon("img/bg/shuoming.png");
		final JLabel jLabel3 = new JLabel(img3);
		jLabel3.setBounds(w*5/12, h/40, 600, 800);
		jLabel3.setVisible(false);
		
		//模式选择按钮与说明按钮
		JButton onePlayerButton = new JButton();
		JButton twoPlayerButton = new JButton();
		JButton magicBoxButton = new JButton();
		JButton magicBoxButton2 = new JButton();
		//地图选择按和返回按钮
		JButton stage1Button = new JButton();
		JButton stage2Button = new JButton();
		JButton stage3Button = new JButton();
		JButton backButton = new JButton();
		
		//单人模式按钮onePlayerButton
		onePlayerButton.setIcon(ElementLoader.getElementLoader().getImageMap().get("rect1"));
		onePlayerButton.setBounds(w/6, h/6, 180, 60);
		onePlayerButton.setBorderPainted(false);
		onePlayerButton.setFocusPainted(false);
		onePlayerButton.setContentAreaFilled(false);
		onePlayerButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameController.setTwoPlayer(false);
				
				onePlayerButton.setVisible(false);
				twoPlayerButton.setVisible(false);
				magicBoxButton.setVisible(false);
				magicBoxButton2.setVisible(false);
				jLabel2.setVisible(false);
				jLabel3.setVisible(false);
				
				stage1Button.setVisible(true);
				stage2Button.setVisible(true);
				stage3Button.setVisible(true);
				backButton.setVisible(true);
			}
		});
		
		//双人模式按钮twoPlayerButton
		twoPlayerButton.setIcon(ElementLoader.getElementLoader().getImageMap().get("rect2"));
		twoPlayerButton.setBounds(w/6, h/3, 180, 60);
		twoPlayerButton.setBorderPainted(false);
		twoPlayerButton.setFocusPainted(false);
		twoPlayerButton.setContentAreaFilled(false);
		twoPlayerButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameController.setTwoPlayer(true);
				
				onePlayerButton.setVisible(false);
				twoPlayerButton.setVisible(false);
				magicBoxButton.setVisible(false);
				magicBoxButton2.setVisible(false);
				jLabel2.setVisible(false);
				jLabel3.setVisible(false);
				
				stage1Button.setVisible(true);
				stage2Button.setVisible(true);
				stage3Button.setVisible(true);
				backButton.setVisible(true);
			}
		});

		//道具说明按钮magicBoxButton
		magicBoxButton.setIcon(ElementLoader.getElementLoader().getImageMap().get("rect3"));
		magicBoxButton.setBounds(w/6, h/2, 180, 60);
		magicBoxButton.setBorderPainted(false);
		magicBoxButton.setFocusPainted(false);
		magicBoxButton.setContentAreaFilled(false);
		magicBoxButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(!jLabel2.isVisible())
				{	
					jLabel2.setVisible(true);
					jLabel3.setVisible(false);//调整代码
				}
				else {
					jLabel2.setVisible(false);
				}
			}
		});
		
		//新增游戏说明按钮设置magicBoxButton2
		magicBoxButton2.setIcon(ElementLoader.getElementLoader().getImageMap().get("rect5"));
		magicBoxButton2.setBounds(w/6, h-h/3, 180, 60);
		magicBoxButton2.setBorderPainted(false);
		magicBoxButton2.setFocusPainted(false);
		magicBoxButton2.setContentAreaFilled(false);
		magicBoxButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			
				if(!jLabel3.isVisible())
				{	jLabel3.setVisible(true);
				jLabel2.setVisible(false);
				}
				else {
					jLabel3.setVisible(false);
				}
			}
		});
		
		//设置地图一选择按钮stage1Button
		stage1Button.setIcon(ElementLoader.getElementLoader().getImageMap().get("stage1"));
		stage1Button.setBounds(w/6, h/6, 180, 60);
		stage1Button.setBorderPainted(false);
		stage1Button.setFocusPainted(false);
		stage1Button.setContentAreaFilled(false);
		stage1Button.setVisible(false);
		stage1Button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameThread.setStageNum(1);
				GameStart.startNewGame();
			}
		});
		
		//设置地图二选择按钮stage2Button
		stage2Button.setIcon(ElementLoader.getElementLoader().getImageMap().get("stage2"));
		stage2Button.setBounds(w/6, h/3, 180, 60);
		stage2Button.setBorderPainted(false);
		stage2Button.setFocusPainted(false);
		stage2Button.setContentAreaFilled(false);
		stage2Button.setVisible(false);
		stage2Button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameThread.setStageNum(2);
				GameStart.startNewGame();
			}
		});
		
		//设置地图三选择按钮stage3Button
		stage3Button.setIcon(ElementLoader.getElementLoader().getImageMap().get("stage3"));
		stage3Button.setBounds(w/6, h/2, 180, 60);
		stage3Button.setBorderPainted(false);
		stage3Button.setFocusPainted(false);
		stage3Button.setContentAreaFilled(false);
		stage3Button.setVisible(false);
		stage3Button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameThread.setStageNum(3);
				GameStart.startNewGame();
			}
		});
		
		//设置返回按钮backButton
		backButton.setIcon(ElementLoader.getElementLoader().getImageMap().get("back"));
		backButton.setBounds(w/6, h-h/3, 180, 60);
		backButton.setBorderPainted(false);
		backButton.setFocusPainted(false);
		backButton.setContentAreaFilled(false);
		backButton.setVisible(false);
		backButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				onePlayerButton.setVisible(true);
				twoPlayerButton.setVisible(true);
				magicBoxButton.setVisible(true);
				magicBoxButton2.setVisible(true);
				
				stage1Button.setVisible(false);
				stage2Button.setVisible(false);
				stage3Button.setVisible(false);
				backButton.setVisible(false);
			}
		});
		
		this.add(onePlayerButton);
		this.add(twoPlayerButton);
		this.add(magicBoxButton);
		this.add(magicBoxButton2);
		this.add(stage1Button);
		this.add(stage2Button);
		this.add(stage3Button);
		this.add(backButton);
		this.add(jLabel3);
		this.add(jLabel2);
		this.add(jLabel);
		
		
		this.setVisible(true);
		this.setOpaque(true);
	}
	
}
